<?php
require_once(dirname(__FILE__)."/../lib/Loader.php");
cfgload("defines");
cfgload("dd");
cload("Smarty.smartyRenderer");
cload("requestProcessor");
cload("dd.ddrow");
/**
 * Description of admDD
 *
 * @author Di_Moon
 */
class admDD extends smartyRenderer {
    protected $_dbh;
    protected $_ddFor;
    protected $_ddrow;
    
    function __construct($dbh) {
        parent::__construct();
        $this->_dbh = $dbh;
        
        $this->_ddFor = requestProcessor::getVar("ddFor", DOPDATA_FOR_FILM);
        $this->assign("ddFor", $this->_ddFor);
        
        $this->_ddrow = new ddrow($this->_dbh, $this->_ddFor);
    }
    
    function render() {
        $_action = requestProcessor::getVar("action");

        if($_action=="default") {
            $_fldname = requestProcessor::getVar("fldname");

            if(requestProcessor::getVar("value")) {
                $value = requestProcessor::getVar("value");
                $this->_ddrow->setDefaultData($_fldname, $value);
                header("Location: ".$_SERVER["SCRIPT_NAME"]."?a=admDD&ddFor=".$this->_ddFor);
                exit();
            }
            
            $_value = requestProcessor::getVar("value", $this->_ddrow->getDefaultData($_fldname));
            
            $this->assign("fldname", $_fldname);
            $this->assign("fldvalue", $_value);
            
            return $this->fetch("admDDfieldDefault.tpl");
        }
        
        if($_action=="form") {
            $_post = requestProcessor::post();
            $this->_ddrow->addField(
                    $_post->get("ddname"), 
                    $_post->get("ddtitle"), 
                    (int)$_post->get("ddtype"));
        }
        
        if($_action=="formTitle") {
            $_fldName = requestProcessor::getVar("fldname");
            $_newTitle = requestProcessor::getVar("newtitle");
            
            $this->_ddrow->updateFieldTitle($_fldName, $_newTitle);
        }
        
        //_print_r($this->_ddrow->getFields());
        
        $this->assign("fieldsList", $this->_ddrow->getFields());
        return $this->fetch("admDDfieldsList.tpl");
    }
}

?>
